package com.hliushi.learn.controller;

import com.hliushi.learn.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName JsonController
 * @Description TODO
 * @Author hliushi
 * @Date 2021/7/5 20:11
 * @Version V1.0
 **/
@RestController     //  == @Controller + @ResponseBody
@RequestMapping(value = "/json")
public class JsonController {

    private static final Logger LOGGER = LoggerFactory.getLogger(JsonController.class);


    @RequestMapping("/user")
    public User getUser() {
        // {
        //      "id": 10000,
        //      "username": "hliushi",
        //      "password": "qwer1234"
        // }
        return new User(10000L, "hliushi", "qwer1234");
    }


    /**
     * @return
     */
    @RequestMapping("/list")
    public List<User> getUserList() {
        // [
        //     {
        //         "id": 10000,
        //         "username": "zid",
        //         "password": "123456"
        //     },
        //     {
        //         "id": 10000,
        //         "username": "pid",
        //         "password": "123456"
        //     }
        // ]
        List<User> list = new ArrayList<>();

        list.add(new User(10000L, "zid", "123456"));
        list.add(new User(10000L, "pid", "123456"));

        return list;
    }

    /**
     * @return
     */
    @RequestMapping("/map")
    public Map<String, Object> getMap() {
        // {
        //     "userInfo": {
        //         "id": 10000,
        //         "username": "zid",
        //         "password": "123456"
        //     },
        //     "debug": "",         // 由于这里配置了Jackson的配置, 将null值转为了 ""
        //     "blogAddress": "http://www.hliushi.cn",
        //     "hobby": [
        //         "java",
        //         "scala",
        //         "spark"
        //     ]
        // }

        HashMap<String, Object> map = new HashMap<>(16);

        User user = new User(10000L, "zid", "123456");
        List<String> list = new ArrayList<>();
        list.add("java");
        list.add("scala");
        list.add("spark");

        map.put("userInfo", user);
        map.put("blogAddress", "http://www.hliushi.cn");
        map.put("hobby", list);
        map.put("debug", null);     // 2021年7月5日20:44:10 添加了JsonConfig配置类

        return map;
    }
}
